/* eslint-disable */
import $ from 'jquery';

export default () => {
    function getCookie(name) {
        //注：只有数字型或者字符型，没有Boolean,空为false,有值哪怕是false都为true
        name = cookieBaseName + name
        var _cookie = document.cookie;
        var firstchar = _cookie.indexOf(name);
        var lastchar;
        if (firstchar != -1) {
            firstchar += name.length + 1;
            lastchar = _cookie.indexOf(";", firstchar);
            lastchar = lastchar == -1 ? _cookie.length : lastchar;
            return unescape(_cookie.substring(firstchar, lastchar));
        }
        return "";

    }
    function setCookie(name, value) {
        name = cookieBaseName + name
        var date = new Date();
        date.setTime(date.getTime() + 365 * 24 * 3600 * 1000);
        document.cookie = name + '=' + escape(value) + ';path=/;  expires=' + date.toGMTString()

    }
    function delCookie(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        document.cookie = name + "=;path=/;expires=" + exp.toGMTString();
    }
    var cookieBaseName = ""

    var App = { evtUrl: "", api: '', wxUrl: "", tokenUrl: '', openid: "" }
    App.ini = function () {
        var v = arguments
        this.evtUrl = v[1];
        this.api = v[2];
        this.wxUrl = "http://www.syruptech.cn/api/wx/";
        this.tokenUrl = v[3] ? v[3] : "";
        this.openid = getCookie("openid");
    }

    App.iniUser = function (callback, callback2) {
        if (!this.openid) {
            var wx = get("wx")
            var d = {}
            if (wx) {
                d.wx = wx
                wxArr = wx.split("|")
                console.log(wxArr)
            }
            this.sendDataBack("ini", d, function (p) {
                if (p.openid) {
                    callback && callback(p)
                    App.openid = p.openid
                    setCookie("openid", p.openid)
                    if (wx) { location.replace(App.evtUrl); }
                } else {
                    if (!wx && App.tokenUrl) { location.replace(App.tokenUrl); }
                }
            })
        } else {
            callback2 && callback2(this.openid)
        }
    }
    App.act = App.sendDataBack = function (act, u, callback) {
        var d = u || {}
        d.act = act
        d.openid = this.openid

        $.get(this.api, d, function (p) {
            callback && callback(p)
        }, "jsonp")
    }
    App.iniWx = function (s) {
        if (!this.wxUrl) {
            console.log("未设置wxUrl"); return;
        }
        $.get(this.wxUrl, "", function (d) {
            wx.config({
                debug: true,
                appId: d.appId, timestamp: d.timestamp, nonceStr: d.nonceStr, signature: d.signature,
                jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'updateAppMessageShareData', 'updateTimelineShareData', 'startRecord', 'stopRecord', 'translateVoice']
            });
            wx.ready(function () { App.iniShare(s); });
            wx.error(function (res) { console.log("wxConfig Fail") });

        }, "jsonp")
    }
    App.iniShare = function (s) {
        wx.updateAppMessageShareData({ title: s.title, desc: s.desc, link: s.link, imgUrl: s.imgUrl, }, function (res) { });
        wx.updateTimelineShareData({ title: s.title2 || s.title, link: s.link, imgUrl: s.imgUrl, }, function (res) { });
        wx.onMenuShareAppMessage({ title: s.title, desc: s.desc, link: s.link, imgUrl: s.imgUrl, success: function (res) { } });
        wx.onMenuShareTimeline({ title: s.title2 || s.title, link: s.link, imgUrl: s.imgUrl, success: function (res) { } });
    }
    var initShare = {
        title: 'test',
        desc: '1234567',
        link: location.href,
        imgUrl: ''
    }
    App.ini();
    App.iniWx(initShare);
};

